home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.1 / Libraries / Intuition / boopsi / demo1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  7.3 KB  |  308 lines

  1. /* demo1.c -- demonstration of         :ts=8
  2.  * Basic Object Oriented Programming System for Intuition
  3.  *
  4.  * This demo shows gadgets and images created using pre-defined
  5.  * boopsi class, but not connected together or bound into a group.
  6.  */
  7.  
  8. /*
  9. Copyright (c) 1989 Commodore-Amiga, Inc.
  10.  
  11. Executables based on this information may be used in software
  12. for Commodore Amiga computers. All other rights reserved.
  13. This information is provided "as is"; no warranties are made.
  14. All use is at your own risk, and no liability or responsibility
  15. is assumed.
  16. */
  17.  
  18. #include "sysall.h"
  19.  
  20. #define D(x)    ;
  21.  
  22. struct  IntuitionBase   *IntuitionBase;
  23. struct  GfxBase         *GfxBase;
  24. struct  Library         *UtilityBase;
  25.  
  26. /* from varargs.c -- interface to NewObjectA()    */
  27. Object    *NewObject();
  28.  
  29. ULONG    myiflags = GADGETDOWN | GADGETUP | CLOSEWINDOW;
  30.  
  31. struct Gadget    *propg;
  32. struct Gadget    *stringg;
  33. struct Gadget    *uparrowg;
  34. struct Gadget    *downarrowg;
  35.  
  36. /* pictures for arrows    */
  37. struct Image    *upimage;
  38. struct Image    *downimage;
  39.  
  40. /* some static layout and setup constants, for now    */
  41. #define GTOP        (44)
  42. #define ARROWLEFT    (180)
  43. #define PWIDTH        (120)    /* width of horizontal propslider    */
  44. #define SWIDTH        (50)
  45. #define PROPRANGE    (20)
  46. #define INITVAL        (0)    /* initial value of string and slider    */
  47.  
  48. enum gadgetids {
  49.     gUp = 1,
  50.     gDown,
  51.     gSlider,
  52.     gString,
  53. };
  54.  
  55. struct TagItem    proptags[] = {
  56.     {GA_TOP,        GTOP},
  57.     {GA_WIDTH,        PWIDTH},    /* height to be specified    */
  58.     {GA_IMMEDIATE,    TRUE},
  59.     {GA_RELVERIFY,    TRUE},
  60.     {GA_ID,        gSlider},
  61.     {PGA_FREEDOM,    FREEHORIZ},
  62.     {PGA_TOP,        INITVAL},    /* "top" in the scroller sense    */
  63.     {PGA_VISIBLE,    1},        /* want an integer value slider    */
  64.     {PGA_TOTAL,        PROPRANGE},
  65.     {TAG_END ,}
  66. };
  67.  
  68. struct TagItem    stringtags[] = {
  69.     {GA_ID,        gString},
  70.     {GA_TOP,        GTOP},
  71.     {GA_WIDTH,        SWIDTH},
  72.     {GA_IMMEDIATE,    TRUE},
  73.     {GA_RELVERIFY,    TRUE},
  74.     {GA_HEIGHT,        12},        /* fix this    */
  75.     {STRINGA_MaxChars,    10},
  76.     {STRINGA_LongVal,    INITVAL},    /* make it an integer gadget */
  77.     {STRINGA_Justification,
  78.                 STRINGRIGHT},
  79.     {TAG_END, }
  80. };
  81.  
  82. struct TagItem    arrowtags[] = {
  83.     {GA_TOP    ,    GTOP},
  84.     {GA_IMMEDIATE,    TRUE},
  85.     {GA_RELVERIFY,    TRUE},
  86.     {TAG_END, }
  87. };
  88.  
  89. struct TagItem    arrows2me[] = {
  90.     {GA_ID, GA_ID},
  91.     {TAG_END, }
  92. };
  93.  
  94. #define MYWINDOWTITLE    "boopsi Demo 1"
  95.  
  96. main()
  97. {
  98.     struct DrawInfo    *GetScreenDrawInfo();
  99.     struct Window     *OpenWindowTags();    /* in varargs.c for now */
  100.  
  101.     struct Gadget    *mygadgets = NULL;
  102.     struct Gadget    *tmpgad;
  103.     struct Window    *w;
  104.     struct DrawInfo    *drinfo;
  105.  
  106.     if (!(UtilityBase=(struct Library *)OpenLibrary("utility.library",36L)))
  107.     { cleanup("no V36 utility library\n"); }
  108.  
  109.     if (!(IntuitionBase = 
  110.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)))
  111.     { cleanup("no V36 intuition library\n"); }
  112.  
  113.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L)))
  114.     { cleanup("no V36 graphics library\n"); }
  115.  
  116.     D( printf("about to openwindow\n") );
  117.     w = OpenWindowTags( NULL, 
  118.         WA_Title,    MYWINDOWTITLE,
  119.         WA_SimpleRefresh, TRUE,
  120.         WA_NoCareRefresh, TRUE,
  121.         WA_DepthGadget,    TRUE,
  122.         WA_DragBar,    TRUE,
  123.         WA_Left,    300,
  124.         WA_Top,        50,
  125.         WA_Width,    280,
  126.         WA_Height,    120,
  127.         WA_IDCMP,    myiflags,
  128.         WA_CloseGadget,    TRUE,
  129.             TAG_END );
  130.     D( printf("window at %lx\n ", w ) );
  131.  
  132.     if ( w == NULL) cleanup( "couldn't open my window.\n");
  133.     drinfo = GetScreenDrawInfo( w->WScreen );
  134.  
  135.     /* get images for the up and down arrows, sensitive
  136.      * to depth and pen specs for current screen (we'll be
  137.      * adding resolution/size selection later).
  138.      */
  139.     upimage = (struct Image *) NewObject( NULL, "sysiclass",
  140.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  141.     SYSIA_Which,    UPIMAGE,
  142.     SYSIA_DrawInfo,    drinfo,
  143.         TAG_END );
  144.  
  145.     downimage = (struct Image *) NewObject( NULL, "sysiclass",
  146.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  147.     SYSIA_Which,    DOWNIMAGE,
  148.     SYSIA_DrawInfo,    drinfo,
  149.         TAG_END );
  150.  
  151.     /* make gadgets, link into list (easier starting with Beta 4) */
  152.     tmpgad = (struct Gadget *) &mygadgets;
  153.  
  154.     downarrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  155.     GA_IMAGE,     downimage,
  156.     GA_LEFT,    ARROWLEFT,
  157.     GA_ID,        gDown,
  158.     GA_PREVIOUS,    tmpgad,
  159.     TAG_MORE,    arrowtags,
  160.  
  161.     TAG_END );
  162.     D( printf("downgadget at %lx\n", downarrowg ));
  163.  
  164.     /* get up/down arrow button gadgets    */
  165.     uparrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  166.     GA_IMAGE,     upimage,
  167.     GA_LEFT,    ARROWLEFT + (downarrowg? downarrowg->Width: 0),
  168.     GA_ID,        gUp,
  169.     GA_PREVIOUS,    tmpgad,
  170.     TAG_MORE,    arrowtags,
  171.     TAG_END );
  172.     D( printf("upgadget at %lx\n", uparrowg ));
  173.  
  174.  
  175.     propg = (struct Gadget *) NewObject( NULL, "propgclass",
  176.     GA_LEFT,    ARROWLEFT - PWIDTH,
  177.     GA_HEIGHT,    downarrowg? downarrowg->Height: 20,
  178.     GA_PREVIOUS,    tmpgad,
  179.     TAG_MORE,    proptags,
  180.     TAG_END );
  181.     D( printf( "prop gadget returned: %lx\n", propg ) );
  182.  
  183.     stringg  = (struct Gadget *) NewObject( NULL, "strgclass",
  184.         GA_LEFT,    propg? (propg->LeftEdge - SWIDTH): 20,
  185.     GA_PREVIOUS,    tmpgad,
  186.     TAG_MORE,    stringtags,
  187.     TAG_END );
  188.     D( printf( "string at %lx\n", stringg ) );
  189.  
  190.     D(printf("objects initialized\n"));
  191.  
  192.     AddGList( w, mygadgets, -1, -1, NULL );
  193.     RefreshGList( mygadgets, w, NULL, -1 );
  194.  
  195.     D( printf("gadgets added and refreshed \n") );
  196.  
  197.     goHandleWindow( w );
  198.  
  199.     RemoveGList( w, mygadgets, -1 );
  200.     FreeScreenDrawInfo( w->WScreen, drinfo );
  201.     CloseWindow( w );
  202.  
  203.     D( printf("dispose\n") );
  204.     while ( mygadgets )
  205.     {
  206.     tmpgad = mygadgets->NextGadget;
  207.     DisposeObject( mygadgets );
  208.     mygadgets = tmpgad;
  209.     }
  210.  
  211.     DisposeObject( upimage);
  212.     DisposeObject( downimage);
  213.     D( printf("have disposed.\n") );
  214.  
  215.     cleanup( "all done" );
  216. }
  217.  
  218. cleanup( str )
  219. char    *str;
  220. {
  221.     if (str) printf("%s\n", str);
  222.  
  223.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  224.     if (GfxBase) CloseLibrary(GfxBase);
  225.     if (UtilityBase) CloseLibrary(UtilityBase);
  226.  
  227.     exit (0);
  228. }
  229.  
  230. /* this variable holds the integer "current value" of the
  231.  * whole little system of gadgets
  232.  */
  233. LONG        currval = 0;
  234.  
  235. goHandleWindow( w )
  236. struct Window    *w;
  237. {
  238.     struct IntuiMessage *imsg;
  239.     struct Gadget    *g;
  240.  
  241.     for(;;)
  242.     {
  243.     WaitPort( w->UserPort );
  244.     while ( imsg = (struct IntuiMessage *) GetMsg( w->UserPort ) )
  245.     {
  246.         switch ( imsg->Class )
  247.         {
  248.         case CLOSEWINDOW:
  249.             ReplyMsg( (struct Message *) imsg );
  250.         return;
  251.  
  252.         case GADGETDOWN:
  253.         D( printf("gadget down, %lx\n", imsg->IAddress ) );
  254.         break;
  255.  
  256.         case GADGETUP:
  257.         g = (struct Gadget *) imsg->IAddress;
  258.         D( printf("gadget up, %lx, id %lx\n",
  259.             imsg->IAddress, g->GadgetID ) );
  260.  
  261.         /* manual updating of integer value.
  262.          * this is made more automatic in subsequent demos
  263.          */
  264.         switch ( g->GadgetID )
  265.         {
  266.         case gUp:
  267.             currval++;
  268.             manualUpdate( w );
  269.             break;
  270.         case gDown:
  271.             currval--;
  272.             manualUpdate( w );
  273.             break;
  274.         case gSlider:
  275.             GetAttr( PGA_TOP, g, &currval );
  276.             manualUpdate( w );
  277.             break;
  278.         case gString:
  279.             GetAttr( STRINGA_LongVal, g, &currval );
  280.             manualUpdate( w );
  281.             break;
  282.         default:
  283.             D( printf("unknown gadget id\n"));
  284.         }
  285.  
  286.         break;
  287.         }
  288.         ReplyMsg( (struct Message *) imsg );
  289.     }
  290.     }
  291. }
  292.  
  293. manualUpdate( w )
  294. struct Window    *w;
  295. {
  296.     /* put currval in legal bounds    */
  297.     currval = max( 0, min( PROPRANGE-1, currval ) );
  298.  
  299.     /* tell interested parties        */
  300.    SetGadgetAttrs( propg, w, NULL, 
  301.                PGA_TOP, currval,
  302.             TAG_END );
  303.  
  304.    SetGadgetAttrs( stringg, w, NULL, 
  305.                STRINGA_LongVal, currval,
  306.             TAG_END );
  307. }
  308.